#include "stm32f10x.h"  // Device header



typedef struct {
    uint16_t Window[21];  
    uint8_t index;      //索引 
    uint32_t sum;        
} Filter,Filter2, Filter3,Filter4;


static Filter filterStruct = {0};
static Filter2 filterStruct2 = {0};
static Filter3 filterStruct3 = {0}; 
static Filter4 filterStruct4 = {0};
static uint8_t Init_Flag = 0;   //标志 

uint16_t Filter_Function(uint16_t NewData,Filter* filter) 
{
    
    if (!Init_Flag) 
		{
        for (int i = 0; i < 21; i++) 
			  {
            filter->Window[i] = 0;
        }
        filter->index = 0;
        filter->sum = 0;
        Init_Flag = 1;
    }
   
    filter->sum-= filterStruct.Window[filterStruct.index];//去除最旧数据
   filter->Window[filterStruct.index] = NewData;          //填入最新数据
    filter->index = (filterStruct.index + 1) % 21;        //环形填充，更新索引
    filter->sum += NewData;                                //计算新的平均值
    return filter->sum / 21;
}

 uint16_t Filter_Return1 (uint16_t NewData) 
{
	   return Filter_Function(NewData,&filterStruct)  ;

}

 uint16_t Filter_Return2 (uint16_t NewData) 
{
	   return Filter_Function(NewData,&filterStruct2)  ;

}

 uint16_t Filter_Return3 (uint16_t NewData) 
{
	   return Filter_Function(NewData,&filterStruct3)  ;

}

uint16_t Filter_Return4 (uint16_t NewData) 
{
	   return Filter_Function(NewData,&filterStruct4)  ;

}